import os
from multiprocessing import Process,Manager
import random


def compute(result_dict):
    '''计算100万个1到100之间的随机数的和'''
    total = sum([random.randint(1,100) for i in range(1000000)])
    result_dict[os.getpid()] = total


def main():
    # 创建一个Manage上下文的环境
    with Manager() as manager:
        result_dict = manager.dict()
        processes = [Process(target=compute,args=(result_dict,)) for i in range(8)]
        for process in processes:
            process.start()
        for process in processes:
            process.join()
        print(result_dict)
if __name__ == '__main__':
    main()